<template>
  <div>
    <section id="login">
      <div class="textAndLoginBox">
        <div class="logo-font">
          <img src="../assets/img/logo-01.svg" class="logo" />
        </div>
        <div class="login-box">
          <div class="title">学员登录</div>
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="login-form">
            <el-form-item prop="username">
              <el-input type="text" v-model="ruleForm.username" autocomplete="off" placeholder="请输入学号"
                suffix-icon="el-icon-user">
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input type="password" 
              v-model="ruleForm.password" 
              autocomplete="off" 
              placeholder="请输入密码"
              @ketup.enter="submitForm('ruleForm')"
              suffix-icon="el-icon-lock">
              </el-input>
            </el-form-item>
            <el-form-item class="pt30">
              <el-button type="primary" @click="submitForm('ruleForm')">
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </section>
    <footer class="home-footer fixed-home-footer">
      <p>
        Copyright © 2003-2019 福州市智慧一加一教育科技有限公司.All Rights Reserved
        湘ICP备2023007847号-3
      </p>
    </footer>
  </div>
</template>


<script>
import NodeRSA from "node-rsa";
import { mapActions } from "vuex";
import EventBus from "../assets/js/eventBus";

const publicKey = `
-----BEGIN PUBLIC KEY-----
  MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAwwgxYqXm5u2yOmDvhFl3
ZUoJXl219eXnQyTT6qaNlU6WmVUPnAwlyHvlfMjGrpLz/rRZl55Nz8K/o3GBKaTY
yx1P6HBt8E1ZYDOcIxBzCSKkA53e2rHeiZtRUuytU5e/hztSW6XPcM6fZbx5Hyxk
ytfP0DvI3fCIV2OSpivKjEkpgLJYfAqFH5SkagVGmXOykwqRseK4RvX+VS+X1Jj2
yvL8YHVecDn1C18yq4bhG9IkIo1vQ+DtgxX1YTrNDuKOIgN2eXHqwxCgXgbehbQf
qywBuU/DeqpLQpw+V1a8zIirHopMvpCLUV4wZQyqiYWlb7YSAB9DeXAysBxtV+Ji
LmdkYt4UD5NqIiVIXmgVkObJ28iYJ82n3C7N35HW9ucOAEBiuH7I2SbDTIl00e84
t4qWG+r8J0iO14cnIYzGgFX5yvmewV5FfspJAjuPNFaM2h9i3+E36Y8bE4D35kQe
+immZDtjGoXKdpooL+WmW20Xu3wX1p6CCX8hjR+J4FkJwIg5yQ8L0L050k9d+wOW
+sEaW+NHyT/woFaGqVbITRfiKj701+C6mOP4msh6DuOU4yCYlKFL2dRSAt8si9/l
QnucnlRafH438b45InVP5i92snroHU2MytrpMtB+jwjTuKvPOd5RDpAX0ZzNzFZL
DgL0DEfVgOHTT8iycgK9GRkCAwEAAQ==
-----END PUBLIC KEY-----`;
export default {
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        if (/\w{6,}/.test(value)) {
          callback();
        } else {
          callback(new Error("用户名至少 6 位字符"));
        }
      }
    };
    var validatePassword = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (/\w{6,}/.test(value)) {
          callback();
        } else {
          callback(new Error("密码至少 6 位字符"));
        }
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
         channel:'XG'
      },
      rules: {
        username: [{ validator: validateUsername, trigger: "blur" }],
        password: [{ validator: validatePassword, trigger: "blur" }],
      },
    };
  },
  methods: {
    ...mapActions(["Login"]),
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.login();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    login() {
      const myEncrypt = new NodeRSA(publicKey);
      myEncrypt.setOptions({ encryptionScheme: "pkcs1" }); // 指定RSA加密协议，pkcs1 或 pkcs1_oaep，默认pkcs1_oaep
      let psw = myEncrypt.encrypt(this.ruleForm.password, "base64"); // 指定输出编码类型为base64
      let data = {
        username: this.ruleForm.username,
        password: psw,
         channel:'XG'
      };
      this.Login(data).then((res) => {
        if (res.code == 200) {
          this.$router.push({
            path: "/",
          });
          // 欢迎来到晓果智学
          // EventBus.$emit("playVoice", { type: 8 });
        }
      });
    },
  },
};
</script>